﻿using HCMS.Const;
using HCMS.Extensions;
using HCMS.Utils;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace HCMS.DomainServiceManage.Cache.Manage
{
    /// <summary>
    /// 管理员 缓存 域服务
    /// </summary>
    public class AdminCacheManager : DomainService
    {
        private readonly IDistributedCache cache;

        public AdminCacheManager(IDistributedCache cache)
        {
            this.cache = cache;
        }

        /// <summary>
        /// 设置登录信息
        /// </summary>
        /// <remarks>
        /// 人员进行登录、更新管理员密码、状态，都需要执行此方法
        /// </remarks>
        /// <param name="adminId">管理员 Id</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="ts">保存时间</param>
        /// <returns></returns>
        public async Task<bool> SetLoginedInfo(int adminId, long timestamp, TimeSpan ts)
        {
            try
            {
                byte[] storeValue = Encoding.UTF8.GetBytes(timestamp.ToString());
                DistributedCacheEntryOptions options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(ts);
                await cache.SetAsync(string.Format(Manage_RedisKey.Logined, adminId), storeValue, options);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <remarks>
        /// 无值、无效，则管理员下线
        /// </remarks>
        /// <param name="adminId">管理员 Id</param>
        /// <returns></returns>
        public async Task<string> GetLoginedInfo(int adminId)
        {
            string key = string.Format(Manage_RedisKey.Logined, adminId);
            string? cacheValue = await cache.GetStringAsync(key);
            if (!string.IsNullOrWhiteSpace(cacheValue))
            {
                return cacheValue;
            }

            return string.Empty;
        }

        /// <summary>
        /// 设置在线状态
        /// </summary>
        /// <param name="adminId">管理员 Id</param>
        /// <param name="isOnline">是否在线。1：在线、0：离线</param>
        /// <returns></returns>
        public async Task<bool> SetOnlineStatus(int adminId, string isOnline)
        {
            try
            {
                var expiredTime = DateTimeOffset.Parse($"{DateTimeOffset.Now:yyyy-MM-dd 23:59:59}");
                var ts = DateTimeUtil.DifferenTimespan(DateTime.Now, expiredTime);

                byte[] storeValue = Encoding.UTF8.GetBytes(isOnline);
                DistributedCacheEntryOptions options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(ts);
                await cache.SetAsync(string.Format(Manage_RedisKey.OnlineStatus, adminId), storeValue, options);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取在线状态
        /// </summary>
        /// <param name="adminId">管理员 Id</param>
        /// <returns>true：在线、false：不在线</returns>
        public async Task<bool> GetOnlineStatus(int adminId)
        {
            string key = string.Format(Manage_RedisKey.OnlineStatus, adminId);
            string? cacheValue = await cache.GetStringAsync(key);
            if (!string.IsNullOrWhiteSpace(cacheValue) && cacheValue.IsSame("1"))
            {
                return true;
            }

            return false;
        }
    }
}
